package com.bawei.day16_messager_server;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyService extends Service {
    Messenger messenger =  new Messenger(new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //TODO 1:接受客户端发来消息
            Bundle bundle = msg.getData();
            String num = bundle.getString("num");
            Toast.makeText(MyService.this, ""+num, Toast.LENGTH_SHORT).show();
            //TODO 2：返回消息
            Messenger clientMessager = msg.replyTo;
            Message message = new Message();
            Bundle bundle1 = new Bundle();
            bundle1.putString("recult","你是傻子吗");
            message.setData(bundle1);
            try {
                clientMessager.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }
    });


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}
